<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="流程定义标识" prop="processDefinitionKey">
        <el-input
          v-model="queryParams.processDefinitionKey"
          placeholder="请输流程定义标识"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="bpmProcessInstanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流程实例id" align="center" prop="processInstanceId" v-if="true" :show-overflow-tooltip="true"/>
      <el-table-column label="流程定义名称" align="center" prop="processInstanceName" :show-overflow-tooltip="true" />
      <el-table-column label="流程定义key" align="center" prop="processDefinitionKey" :show-overflow-tooltip="true"/>
      <el-table-column label="流程定义版本" align="center" prop="processDefinitionVersion" />
      <el-table-column label="流程发起人ID" align="center" prop="startUserId" />
      <el-table-column label="流程发起人" align="center" prop="startUserNickName" />
<!--      <el-table-column label="流程启动时间" align="center" prop="startTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="当前办理人" align="center" prop="currAssignee" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['flowable:bpmProcessInstance:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleInfo(scope.row)"
            v-hasPermi="['flowable:bpmProcessInstance:edit']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改流程实例对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-table v-loading="loading" :data="processDefinitionList" @selection-change="handleSelectionChange">
        <el-table-column label="流程定义名称" align="center" prop="processDefinitionName" />
        <el-table-column label="流程定义标识" align="center" prop="processDefinitionKey" />
        <el-table-column label="挂起或激活 " align="center" prop="processDefinitionState" />
        <el-table-column label="流程xml名称" align="center" prop="xmlName" />
        <el-table-column label="流程部署时间" align="center" prop="processDeploymentTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.processDeploymentTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="描述" align="center" prop="description" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleStartProcessInstance(scope.row)"
            >发起流程</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {
  listFinishBpmProcessInstance,
  getBpmProcessInstance,
 deleteHistoryProcessInstance
} from "@/api/flowable/bpmProcessInstance";

export default {
  name: "BpmProcessInstance",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 流程实例表格数据
      bpmProcessInstanceList: [],
      processDefinitionList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        processInstanceName: undefined,
        processDefinitionKey: undefined,
        processDefinitionVersion: undefined,
        startUserId: undefined,
        startUserNickName: undefined,
        processInstanceState: undefined,
        businessKey: undefined,
        startTime: undefined,
        currAssignee: undefined,
        currAssigneeId: undefined,
        actBusinessStatus: undefined
      },
      defineQueryParams:{
        pageNum: 1,
        pageSize: 10,
      },

      processInstanceInfo:{},
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        processInstanceId: [
          { required: true, message: "流程实例id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询流程实例列表 */
    getList() {
      this.loading = true;
      listFinishBpmProcessInstance(this.queryParams).then(response => {
        this.bpmProcessInstanceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        processInstanceId: undefined,
        processInstanceName: undefined,
        processDefinitionKey: undefined,
        processDefinitionVersion: undefined,
        startUserId: undefined,
        startUserNickName: undefined,
        processInstanceState: undefined,
        businessKey: undefined,
        startTime: undefined,
        currAssignee: undefined,
        currAssigneeId: undefined,
        actBusinessStatus: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.processInstanceId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },


    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除流程实例为"' + row.processInstanceName + '"的数据项？').then(() => {
        this.loading = true;
        return deleteHistoryProcessInstance(row.processInstanceId);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /**
     * 流程实例详细信息
     * @param row
     */
    handleInfo(row){
      this.$router.push({
        name:'bpmnProcessDetail',
        query:{
          processInstanceId:row.processInstanceId
        }
      })
    }
  }
};
</script>
